<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.name" size="default" placeholder="请输入用户名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" v-auth="'user:add'" @click="onOpenAddUser('add')">
					<el-icon>
						<ele-Plus />
					</el-icon>
					添加
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" border>
				<el-table-column type="index" label="序号" align="center" width="70" />
				<el-table-column prop="name" label="姓名" align="center" show-overflow-tooltip min-width="120"></el-table-column>
				<el-table-column prop="loginuser" label="账号" align="center" show-overflow-tooltip min-width="180"></el-table-column>
				<el-table-column prop="roleName" label="角色" align="center" show-overflow-tooltip min-width="150" v-if="!useUserInfo().userInfos.companyid"></el-table-column>
				<el-table-column prop="iscompanymanager" label="代理商管理员" align="center" show-overflow-tooltip min-width="120" v-if="!useUserInfo().userInfos.companyid">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.iscompanymanager">是</el-tag>
						<el-tag type="info" v-else>否</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="telphone" label="手机号" align="center" show-overflow-tooltip min-width="120"></el-table-column>
				<el-table-column prop="cnum" label="购买PC软件数量" align="center" show-overflow-tooltip min-width="150">
					<template #default="scope">
						<el-button type="primary" text @click="codeRef.openDialog(scope.row.telphone)">{{ scope.row.cnum }}</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="用户状态" align="center" show-overflow-tooltip min-width="100">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="loginTime" label="上次登录时间" align="center" show-overflow-tooltip width="160">
					<template #default="scope">
						<div v-if="scope.row.loginTime">{{ formatDate(new Date(scope.row.loginTime),'YYYY-mm-dd HH:MM:SS') }}</div>
					</template>
				</el-table-column>
				<el-table-column prop="createAt" label="创建时间" align="center" show-overflow-tooltip width="170">
					<template #default="scope">
						{{ formatDate(new Date(scope.row.createAt),'YYYY-mm-dd HH:MM:SS') }}
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="150" fixed="right" v-auths="['user:edit', 'user:del']">
					<template #default="scope">
						<el-button :disabled="scope.row.name === 'admin'" size="small" type="warning" v-auth="'user:edit'" @click="onOpenEditUser('edit', scope.row)"
							>编辑</el-button
						>
						<el-button :disabled="scope.row.name === 'admin'" size="small" type="danger" v-auth="'user:del'" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页组件 -->
			<Pagination 
				class="mt15"
				:total="state.tableData.total"
				:page="state.tableData.param.pageNum"
				:limit="state.tableData.param.pageSize"
				@pagination="pageChange"
			/>
		</el-card>
		<AddUser ref="dialogRef" @refresh="getTableData()" />
		<CodeList ref="codeRef" ></CodeList>
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { useUserInfo } from '/@/stores/userInfo';
import { formatDate } from '/@/utils/formatTime';
// 后台接口
import { systemApi } from '/@/api/system/index';
// 引入组件
const AddUser = defineAsyncComponent(() => import('./modal/addUser.vue'));
const CodeList = defineAsyncComponent(() => import('./modal/codeList.vue'));

// 定义变量内容
const dialogRef = ref();
const codeRef = ref();
const state = reactive<SysUserState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
      name: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	let res = await systemApi().getUserList(state.tableData.param)
	state.tableData.data = res.datas.list;
	state.tableData.total = res.datas.count;
	state.tableData.loading = false;
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
	dialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
	dialogRef.value.openDialog(type, row);
};
// const openCodeModal = () => {
// 	codeRef.value.openDialog();
// }
// 删除用户
const onRowDel = (row: RowUserType) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.loginuser}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await systemApi().delUser({id: row.id});
			if(res.success){
				getTableData();
				ElMessage.success("删除成功");
			}else{
				ElMessage.error("删除失败");
			}
		})
		.catch(() => {});
};
// 分页组件改变
const pageChange = (pageObj: {pageNum:number,pageSize:number}) => {
	state.tableData.param.pageNum = pageObj.pageNum;
	state.tableData.param.pageSize = pageObj.pageSize;
	getTableData();
}
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
